<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="油卡卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardNumber">
<!--              <a-input :disabled="formDisabled || disableOther" v-model="model.cardNumber" placeholder="请输入油卡卡号" ></a-input>-->
              <a-input :disabled="formDisabled" v-model="model.cardNumber" placeholder="请输入油卡卡号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agent">
<!--              <j-dict-select-tag :disabled="formDisabled || disableOther" type="list" v-model="model.agent" dictCode="oil_card_subsidy_agent" placeholder="请选择经办人" />-->
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.agent" dictCode="oil_card_subsidy_agent" placeholder="请选择经办人" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="领用人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardRecipient">
<!--              <select-user-by-dep  :disabled="formDisabled || disableOther"  :multi="false" v-model="model.cardRecipient" @back="changeHandle" ></select-user-by-dep>-->
              <select-user-by-dep  :disabled="formDisabled"  :multi="false" v-model="model.cardRecipient" @back="changeHandle" ></select-user-by-dep>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="领用人所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdeptName" >
              <!-- <select-user-by-dep  :disabled=formDisabled  :multi="false" v-model="model.deptId" @change="changeHandle"></select-user-by-dep> -->
              <!-- <input type="text"  v-model="model.deptId" /> -->
<!--              <a-input  :disabled="formDisabled || disableOther" v-model="model.rdeptName" placeholder="请输入部门" disabled></a-input>-->
<!--              <a-input  :disabled="formDisabled || disableOther" v-model="model.rdeptName" placeholder="请输入部门" disabled></a-input>-->
              <a-input  :disabled="formDisabled || disableOther" v-model="model.rdeptName" placeholder="请输入部门"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" hidden>
            <a-form-model-item  label="领用人所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardRecipientDeptId" >
              <!-- <select-user-by-dep  :disabled=formDisabled  :multi="false" v-model="model.deptId" @change="changeHandle"></select-user-by-dep> -->
              <!-- <input type="text"  v-model="model.cardRecipientDeptId" disabled> -->
              <!-- <a-input  v-model="model.cardRecipientDeptId" placeholder="请输入所属部门" disabled></a-input> -->
              <j-select-depart :disabled="formDisabled || disableOther" v-model="model.cardRecipientDeptId" disabled  />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="油卡开户公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openCompany">
<!--              <j-dict-select-tag :disabled="formDisabled || disableOther" type="list" v-model="model.openCompany" dictCode="oil_openCompany" placeholder="请选择油卡开户公司" />-->
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.openCompany" dictCode="oil_openCompany" placeholder="请选择油卡开户公司" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardStatus">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.cardStatus" dictCode="oil_card_status" placeholder="请选择状态"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
<!--              <a-textarea :disabled="formDisabled || disableOther" :max-length="500" v-model="model.remark" rows="4" placeholder="请输入备注" />-->
              <a-textarea :disabled="formDisabled" :max-length="500" v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'

  export default {
    name: 'ErpOilCardManagerForm',
    components: {
      SelectUserByDep
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
    },
    data () {
      return {
        model:{
          deptName: ''
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          cardNumber: [
              { required: true, message: '请输入油卡卡号!',trigger: 'blur'},
          ],
          rdeptName: [
              { required: true, message: '请输入领用人所属部门!',trigger: 'blur'},
          ],
          agent: [
              { required: true, message: '请选择经办人!',trigger: 'change'},
          ],
          cardRecipient: [
              { required: true, message: '请选择领用人!',trigger: 'change'},
          ],
          openCompany: [
              { required: true, message: '请选择油卡开户公司!',trigger: 'change'},
          ],
          // cardRecipientDeptId: [
          //     { required: true, message: '请输入所属部门!',trigger: 'change'},
          // ],
          cardStatus: [
              { required: true, message: '请选择状态!',trigger: 'change'},
          ],
        },
        url: {
          add: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/add",
          edit: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/edit",
          queryById: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/queryById",
          list: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/list",
        },
        disableOther: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      //人员选择带出部门
      changeHandle(item){
        // console.log('滴滴滴',item[0]);
        console.log('滴滴滴',item[0].orgCodeTxt);
        console.log('完整数据为：',item[0]);
        this.$set(this.model,'deptName',item[0].orgCodeTxt)
        this.$set(this.model,'cardRecipientDeptId',item[0].deptId)
        this.$set(this.model,'rdeptName',item[0].orgCodeTxt)
      },
      add (disableOther) {
        this.edit(this.modelDefault);
        this.disableOther = disableOther
      },
      edit (record,disableOther) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.disableOther = disableOther
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
                that.$emit('unButtonDisable');
              }).finally(() => {
              that.confirmLoading = false;
            })
          }else {
            that.$emit('unButtonDisable');
            }

        })
      },
    }
  }
</script>
